﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class select_seat_map : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public int RetreiveSeatMap(long bk_id, int pax_sn, int trip_sn, string user,bool low_priority)
    {
        CRSI.Ticket inst = new CRSI.Ticket();
        int ret_code = 0;
        string ret_msg;
        DataSet ds;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        
        if (ret_code < 0)
        {
            return 1;
        }

        //int ret_code;
        //SqlCommand cmd = new SqlCommand();
        //cmd.CommandText = "bll_tkt_get_trip_seat_map";
        //SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", bk_id);
        //para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
        //para = cmd.Parameters.AddWithValue("@trip_sn", trip_sn);
        //para = cmd.Parameters.AddWithValue("@user_airline", user_airline);
        //para = cmd.Parameters.AddWithValue("@office", office);
        //DataTable dtMap = new DataTable();
        try
        {
            string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(bk_id.ToString() + "|" + pax_sn.ToString() + "|" + trip_sn.ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
            ds = inst.GetTripSeatMap(bk_id, pax_sn, trip_sn, user, sign, out ret_code, out ret_msg);
            //ret_code = TinyDAL.Exec(ref cmd, ref dtMap);

            if (ret_code != 0)
                return ret_code;
            
            tabSeatMap.Rows.Clear();
            TableRow tempRow = new TableRow();
            TableCell tempCell;
            Int16 last_col = -1;
            string space_type, seat_status/*, inf_seat*/;
            int curr_x = 1;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (last_col != Convert.ToInt16(row["y"])/* || last_col == max_y + 1*/)
                {
                    tabSeatMap.Rows.Add(tempRow);
                    if (last_col - 1 != Convert.ToInt16(row["y"]))
                    {
                        for (last_col--; last_col > Convert.ToInt16(row["y"]); last_col--)
                        {
                            tempRow = new TableRow();
                            tempCell = new TableCell();
                            tempCell.HorizontalAlign = HorizontalAlign.Left;
                            tempCell.Text = "<b>"+last_col.ToString()+"</b>";
                            tempRow.Cells.Add(tempCell);
                            tabSeatMap.Rows.Add(tempRow);
                        }
                    }

                    tempRow = new TableRow();
                    last_col = Convert.ToInt16(row["y"]);
                    tempCell = new TableCell();
                    tempCell.Text = "<b>" + last_col.ToString() + "</b>";
                    tempCell.HorizontalAlign = HorizontalAlign.Left;
                    tempRow.Cells.Add(tempCell);

                    curr_x = 1;
                }
                if (curr_x != Convert.ToInt16(row["x"]))
                {
                    for (; curr_x < Convert.ToInt16(row["x"]); curr_x++)
                    {
                        tempCell = new TableCell();
                        tempCell.Text = "&nbsp;";
                        tempRow.Cells.Add(tempCell);
                    }
                }
                //if (last_col != Convert.ToInt16(row["y"]) && last_col != -1)
                //{
                //    tabSeatMap.Rows.Add(tempRow);
                //    tempRow = new TableRow();
                //    last_col = Convert.ToInt16(row["y"]);
                //}
                //if (last_col == -1)
                //{
                //    last_col = Convert.ToInt16(row["y"]);
                //}

                tempCell = new TableCell();
                space_type = row["cell_type"].ToString();
                string buf1, buf2;
                if (space_type == "S")//seat
                {
                    seat_status = row["status"].ToString();
                    //if (row["attributes"].ToString().IndexOf('I') >= 0)
                    //    inf_seat = "(I)";
                    //else
                    //    inf_seat = "";
                    buf2 = "type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" +row["row"].ToString() + row["col"].ToString()+"|"+ row["cabin"].ToString()+"|"+row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd();
                    switch (seat_status)
                    {
                        case "A"://available
                            if ((row["ori_airport"].ToString().TrimEnd() == "*" || row["ori_airport"].ToString().TrimEnd() == row["pax_ori_airport"].ToString().TrimEnd())
                                && (row["des_airport"].ToString().TrimEnd() == "*" || row["des_airport"].ToString().TrimEnd() == row["pax_des_airport"].ToString().TrimEnd())
                                && (row["pax_cabin"].ToString() == row["cabin"].ToString())
                                && (row["class"].ToString().TrimEnd() == "*" || row["class"].ToString().TrimEnd() == row["pax_class"].ToString().TrimEnd())
                                && (row["seat_type"].ToString().TrimEnd() == "*" || row["seat_type"].ToString().TrimEnd() == row["pax_seat_type"].ToString().TrimEnd())
                                /*&& (!Convert.ToBoolean(row["hold_inf"]) || Convert.ToBoolean(row["hold_inf"]) && inf_seat!="")*/)  //考虑到报婴座位稀缺，由后台人员通过锁位管理，不自动处理！那么旅客名单中报婴标志就没什么用处了。
                            {
                                buf1 = "<input name='SEAT' style='background-color:green' ";
                                //tempCell.Text = "<input name='SEAT' style='background-color:green' type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd() + "|" + row["row"].ToString() + row["col"].ToString();
                            }
                            else
                            {
                                buf1 = "<input name='SEAT' style='background-color:red' ";
                                if (low_priority)
                                    buf1 = buf1 + "disabled ";   //低优先级的旅客预选座位，不允许跨规则选位！
                                //tempCell.Text = "<input name='SEAT' style='background-color:yellow' type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd() + "|" + row["row"].ToString() + row["col"].ToString();
                            }
                            break;
                        case "O"://occupied
                            if (Convert.IsDBNull(row["ori_row"]) || Convert.IsDBNull(row["ori_col"]) || Convert.ToInt16(row["ori_row"]) != Convert.ToInt16(row["row"]) || row["ori_col"].ToString() != row["col"].ToString())
                                buf1 = "<input name='SEAT' style='background-color:blue' disabled ";
                            else if (Convert.ToInt16(row["ori_row"]) == Convert.ToInt16(row["row"]) && row["ori_col"].ToString() == row["col"].ToString())
                                buf1 = "<input name='SEAT' style='background-color:yellow' ";  //可重复选自己已选的座位，以便取消预选座位
                            else //不可能了吧？
                                buf1 = "<input name='SEAT' style='background-color:gray' disabled ";
                            //tempCell.Text = "<input name='SEAT' style='background-color:red' disabled type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd() + "|" + row["row"].ToString().Trim() + row["col"].ToString();
                            break;
                        //case "B":
                        //    //tempCell.Text = "[B]" + row["row"].ToString() + row["col"].ToString();
                        //    tempCell.Text = "<input name='SEAT' type='radio' value='" /*+ row["tail_no"].ToString() + "|" + row["airline"].ToString() + "|" + row["flt_date"].ToString() + "|" + row["flt_no"].ToString() + "|" */+ row["row"].ToString() + "|" + row["col"].ToString() + "'><font color='red'>" + space_type + "|" + row["row"].ToString() + row["col"].ToString() + "</font>";
                        //    break;
                        default://impossible!
                            buf1 = "<input name='SEAT' style='background-color:gray' disabled ";
                            //tempCell.Text = row["row"].ToString() + row["col"].ToString()+"?";
                            break;
                    }
                    tempCell.Text = buf1 + buf2;

                }
                else//=='R' //Road
                {
                    tempCell.Text = "&nbsp;";
                }
                tempRow.Cells.Add(tempCell);
                curr_x = Convert.ToInt16(row["x"]) + 1;
            }
            //dtMap.Clear();
            tabSeatMap.Rows.Add(tempRow);

            tempRow = new TableRow();
            tempCell = new TableCell();
            tempCell.Text = "<b>Y/X</b>";
            tempRow.Cells.Add(tempCell);
            for (ret_code = 1; ret_code <= curr_x-1; ret_code++)
            {
                tempCell = new TableCell();
                tempCell.Text = "<b>" + ret_code.ToString() + "</b>";
                tempCell.HorizontalAlign = HorizontalAlign.Center;
                tempRow.Cells.Add(tempCell);
            }
            tabSeatMap.Rows.Add(tempRow);

            return 0;
        }
        catch (Exception ex)
        {
            return 100;
            //if (Convert.ToInt16(Application["HTTP_PORT"]) == 1122)
                //lblError.Text = ex.Message;
            //else
                //lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
        }
    }
}